<template>
  <view class="questionnaireCard">
    <view class="cardContent">
      <view class="cardHeader">
        <view class="titleRow">
          <view class="title">{{ item.title }}
            <s-icon name="edit" size="18" color="#47c5c4" @click="$emit('edit', item)" class="editIcon" />
          </view>
          <view class="statusTag" :class="processedItem.statusClass">
            {{ getStatusText(item.status) }}
          </view>
        </view>
        <view class="metaInfo">
          <view class="description">{{ item.description || "暂无描述" }}</view>
          <view class="bottomRow">
            <view class="createTime">{{ formatDate(item.created_at) }}</view>
            <view class="answerCount">已收集: {{ item.answer_count }} 份</view>
          </view>
        </view>
      </view>
      <!-- @click.stop="$emit('share', item.id)" -->
      <view class="cardFooter">
        <view class="actionGroup">
          <view class="textBtn editBtn" @click="$emit('edit-page', item.id)">编辑问卷 </view>
          <view class="textBtn shareBtn" @click="$emit('preview', item)">查看问卷</view>
          <view class="moreActions" @click.stop="$emit('toggle-menu', item.id)">
            <s-icon name="ellipsis" size="18" color="#909399" :style="{ transform: `rotate(90deg)` }" />
            <view class="moreMenu" v-if="showMenu">
              <!-- @click.stop="share" -->
              <view class="menuItem" style="position:relative" @click="yulan(item)">
                预览试卷
              </view>
              <!-- <view class="menuItem" style="position:relative">
                分享试卷

                <button type="primary" style="position: absolute;width:100%;
                height:100%;
                opacity: 0;top:0;
                left:0;" @click="shareToFriend()" open-type="share">邀请好友</button>

              </view> -->
              <view class="menuItem" @click.stop="$emit('copy', item)">复制问卷</view>
              <view class="menuItem" @click.stop="$emit('rename', item)">重命名</view>
              <view class="menuItem" @click.stop="$emit('delete', item.id)">删除问卷</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <popup ref="popup" type="bottom">
      <sharebtn :itemid="item.id"></sharebtn>
    </popup>
  </view>
</template>

<script>
import popup from '@/components/uni-popup/uni-popup.vue';
import sharebtn from '@/components/share-btn/share-btn.vue';
export default {
  data() {
    return {
      shareContent: {
        title: '默认分享标题',
        path: '/pages/index/index',
        imageUrl: '/static/share-default.png'
      }
    }
  },
  props: {
    item: {
      type: Object,
      required: true
    },
    showMenu: {
      type: Boolean,
      default: false
    }
  },
  components: {
    popup, sharebtn
  },
  computed: {
    processedItem() {
      return {
        ...this.item,
        statusClass: this.getStatusClass(this.item.status)
      };
    }
  },
  onShow() {
    console.log(345)
    // 获取当前页面实例
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];

    // 构建页面路径（添加前缀"/"）
    let path = '/' + currentPage.route;

    // 获取页面参数
    const options = currentPage.options;
    let params = [];

    // 将参数拼接为查询字符串
    for (let key in options) {
      params.push(`${key}=${options[key]}`);
    }

    // 如果有参数，添加到路径中
    if (params.length > 0) {
      path += '?' + params.join('&');
    }
    console.log(path)
    // // 设置分享路径
    // this.shareContent.path = path;
    // console.log(path)
    // this.shareContent.title = "问卷调查"
    // this.shareContent.imageUrl = "https://yingdedao.cn/api//attachment/2025-09/68beed30d07bc.png"
    // 页面加载时设置分享配置

  },
  // onShareAppMessage() {
  //   console.log(222222222222222)
  //   // 处理分享好友
  //   // tranferUrl:
  //   return {
  //     title: this.item.title,
  //     path: "/pages/wenquan/wenquan?id" + this.item.id,
  //     imageUrl: this.shareContent.imageUrl,
  //     success: (res) => {
  //       uni.showToast({
  //         title: '分享成功',
  //         icon: 'success'
  //       });
  //     },
  //     fail: (err) => {
  //       uni.showToast({
  //         title: '分享失败',
  //         icon: 'none'
  //       });
  //     }
  //   }
  // },
  methods: {
    yulan(item) {
      uni.navigateTo({
        url: "/pages/wenquan/wenquan?id=" + item.id
      });
    },

    // 自定义分享方法
    shareToFriend() {
      console.log(234)
      uni.share({
        provider: "weixin",
        scene: "WXSceneSession",
        type: 0,
        title: "万能工具包分享",
        summary: "我刚发现这个超实用的工具应用...",
        imageUrl: "https://example.com/logo.png",
        success: function (res) {
          console.log("分享成功:" + JSON.stringify(res));
        }
      });
    },
    // 自定义分享内容方法
    customizeShare() {
      uni.showModal({
        title: '自定义分享内容',
        content: '请输入分享标题和图片',
        editable: true,
        placeholderText: '分享标题',
        success: (res) => {
          if (res.confirm && res.content) {
            this.shareContent.title = res.content;
            uni.showToast({
              title: '设置成功',
              icon: 'success'
            });
          }
        }
      });
    },

    share() {
      this.$refs.popup.open()
    },
    getStatusText(status) {
      const statusMap = {
        draft: "草稿",
        published: "已发布",
        closed: "已关闭",
      };
      return statusMap[status] || "未知状态";
    },
    getStatusClass(status) {
      const classMap = {
        draft: "status-draft",
        published: "status-published",
        closed: "status-closed",
      };
      return classMap[status] || "";
    },
    formatDate(dateString) {
      if (!dateString) return "";
      const date = new Date(dateString);
      return `${date.getFullYear()}-${(date.getMonth() + 1)
        .toString()
        .padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}`;
    }
  }
};
</script>

<style scoped>
/* 保持原有的样式不变 */
.questionnaireCard {
  margin-bottom: 20rpx;
  border-radius: 12rpx;
  background: #fff;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.cardContent .cardHeader {
  padding: 24rpx;
}

.titleRow {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16rpx;
}

.title {
  font-size: 34rpx;
  font-weight: bold;
  color: #333;
  flex: 1;
  display: flex;
  align-items: center;
}

.editIcon {
  margin-left: 20rpx;
}

.statusTag {
  font-size: 24rpx;
  padding: 4rpx 12rpx;
  border-radius: 20rpx;
  margin-left: 20rpx;
}

.status-draft {
  background-color: #f0f0f0;
  color: #909399;
}

.status-published {
  background-color: #e8f4ff;
  color: #409eff;
}

.status-closed {
  background-color: #fef0f0;
  color: #f56c6c;
}

.metaInfo {
  display: flex;
  flex-direction: column;
}

.description {
  font-size: 28rpx;
  color: #999;
  margin-bottom: 16rpx;
}

.bottomRow {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.createTime,
.answerCount {
  font-size: 26rpx;
  color: #999;
}

.cardFooter {
  border-top: 1rpx solid #eee;
}

.actionGroup {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.textBtn {
  font-size: 28rpx;
  border-radius: 6rpx;
  height: 84rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-grow: 1;
  border-right: #eee 1px solid;
}

.editBtn {
  color: #47c5c4;
}

.shareBtn {
  color: var(--v-theme);
}

.moreActions {
  position: relative;
  width: 84rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.moreMenu {
  position: absolute;
  right: 0;
  top: 40rpx;
  background: #fff;
  border-radius: 8rpx;
  box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1);
  z-index: 10;
  padding: 10rpx 0;
  min-width: 180rpx;
}

.menuItem {
  padding: 16rpx 24rpx;
  font-size: 28rpx;
  color: #666;
}

.menuItem:hover {
  background: #f5f5f5;
}
</style>